package com.iamalive;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.content.Context;

public class UpdateMyDetailsActivity extends Activity {

	private EditText mEditTextName;
	private EditText mEditTextUsername;
	private EditText mEditTextPassword;
	private EditText mEditTextImId;
	private EditText mEditTextPhoneNum;
	private EditText mEditTextAddress;
	private EditText mEditTextCity;
	private EditText mEditTextState;
	private EditText mEditTextCountry;
	private EditText mEditTextZip;
	private Button mButtonUpdate;
	
	private String mName;
	private String mEmailId;
	private String mPassword;
	private String mImId;
	private String mPhoneNum;
	private String mAddress;
	private String mCity;
	private String mState;
	private String mCountry;
	private String mZip;
	
	Context mContext;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_my_details);
		mContext = getApplicationContext();
		
		mEditTextName = (EditText) findViewById(R.id.editTextUpdateMyDetailsName);
		mEditTextUsername = (EditText) findViewById(R.id.editTextUpdateMyDetailsUsername);
		mEditTextPassword = (EditText) findViewById(R.id.editTextUpdateMyDetailsPassword);
		mEditTextImId = (EditText) findViewById(R.id.editTextUpdateMyDetailsMessengerId);
		mEditTextPhoneNum = (EditText) findViewById(R.id.editTextUpdateMyDetailsPhoneNum);
		mEditTextAddress = (EditText) findViewById(R.id.editTextUpdateMyDetailsAddress);
		mEditTextCity = (EditText) findViewById(R.id.editTextUpdateMyDetailsCity);
		mEditTextState = (EditText) findViewById(R.id.editTextUpdateMyDetailsState);
		mEditTextCountry = (EditText) findViewById(R.id.editTextUpdateMyDetailsCountry);
		mEditTextZip = (EditText) findViewById(R.id.editTextUpdateMyDetailsPostalCode);
		
		mButtonUpdate = (Button) findViewById(R.id.buttonUpdateMyDetailsUpdate);
		
		mButtonUpdate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                onUpdateButtonClicked();
            }
        });
		
		populateFieldsWithExistingValues();
	}


	private void populateFieldsWithExistingValues() {
		LocalCache localCache = LocalCache.getSingleInstance();
		mEditTextName.setText(localCache.mUserDetails.name);
		mEditTextUsername.setText(localCache.mUserDetails.emailId);
		mEditTextPassword.setText(localCache.mUserDetails.password);
		mEditTextImId.setText(localCache.mUserDetails.imId);
		mEditTextPhoneNum.setText(localCache.mUserDetails.phone);
		mEditTextAddress.setText(localCache.mUserDetails.address);
		mEditTextCity.setText(localCache.mUserDetails.city);
		mEditTextState.setText(localCache.mUserDetails.state);
		mEditTextCountry.setText(localCache.mUserDetails.country);
		mEditTextZip.setText(localCache.mUserDetails.zip);
	}
	
	
	private void onUpdateButtonClicked() {
		mName = mEditTextName.getText().toString().trim();
		//mEmailId = mEditTextUsername.getText().toString();
		mPassword = mEditTextPassword.getText().toString().trim();
		mImId = mEditTextImId.getText().toString().trim();
		mPhoneNum = mEditTextPhoneNum.getText().toString().trim();
		mAddress = mEditTextAddress.getText().toString().trim();
		mCity = mEditTextCity.getText().toString().trim();
		mState = mEditTextState.getText().toString().trim();
		mCountry = mEditTextCountry.getText().toString().trim();
		mZip = mEditTextZip.getText().toString().trim(); 
				
		if(validateFields() == true) {
			if(UpdateUserDetails() == true) {
				finish();
			} else {
				//TODO handle the issues here
			}
		} else {
			//TODO: show dialog box with error messages
		}
	}

	private boolean validateFields() {
		boolean bRetVal = true;
		String toastMessage = new String();

		if(mName.isEmpty()) {
			toastMessage = "Name field can't be empty...";
			Utils.showToast(mContext, toastMessage);
			return false;
		}
		
		if(mPassword.isEmpty()) {
			toastMessage = "Password field can't be empty...";
			Utils.showToast(mContext, toastMessage);
			return false;			
		}
		
		if((mPhoneNum.isEmpty() == false) && (Utils.isValidPhoneNum(mPhoneNum) == false)) {
			toastMessage = "Phone no. is not valid...";
			Utils.showToast(mContext, toastMessage);
			return false;			
		}
		
		if((mImId.isEmpty() == false) && (Utils.isValidImId(mImId) == false)) {
			toastMessage = "IM ID is not valid...";
			Utils.showToast(mContext, toastMessage);
			return false;			
		}
		
		if((mZip.isEmpty() == false) && (Utils.isValidZip(mZip) == false)) {
			toastMessage = "Zip code is not valid...";
			Utils.showToast(mContext, toastMessage);
			return false;			
		}
		
		return bRetVal;
	}
	
	private boolean UpdateUserDetails() {
		boolean bRetVal = true;
		//TODO: Make a web-service call here
		LocalCache localCache = LocalCache.getSingleInstance();
		
		localCache.mUserDetails.name = mName;
		localCache.mUserDetails.password = mPassword;
		localCache.mUserDetails.imId = mImId;
		localCache.mUserDetails.phone = mPhoneNum;
		localCache.mUserDetails.address = mAddress;
		localCache.mUserDetails.city = mCity;
		localCache.mUserDetails.state = mState;
		localCache.mUserDetails.country = mCountry;
		localCache.mUserDetails.zip = mZip;
		
		return bRetVal;
	}
	
    /**
     * Called when this activity is about to be destroyed by the system.
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
